package com.min.star.service.impl;

import java.util.Arrays;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.min.star.mapper.TeacherMapper;
import com.min.star.model.Teacher;
import com.min.star.service.ITeacherService;

@Service
public class TeacherServiceImpl implements ITeacherService {

	@Autowired
	private TeacherMapper teacherMapper;

	@Override
	public List<Teacher> selectTeacherList() {
		return this.teacherMapper.selectList(null);
	}
	
	@Override
	public Page<Teacher> selectPage(Page<Teacher> page,Teacher teacher) {
		return this.teacherMapper.selectPage(page, null);
	}
	
	@Override
	public Page<Teacher> findPage(Page<Teacher> page, Teacher teacher) {
		return teacherMapper.findPage(page, teacher);
	}

	
	@Override
	public int delete(Long id) {
		return this.teacherMapper.deleteById(id);
	}
	@Override
	public int delete(String ids) {
		return this.teacherMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
	}

	@Override
	public int add(Teacher teacher) {
		return teacherMapper.insert(teacher);
	}

	/**
	 * 当对象publisher被新增后，在mybatisplus中，只需要获取一下该对象的id，
	 * 类似于新的id信息，就会被设置到该对象中
	 */
	@Override
	public Teacher save(Teacher teacher) {
		int ret = teacherMapper.insert(teacher);
		Long id = teacher.getId(); 
		Teacher info = teacherMapper.selectById(id);
		return info;
	}

	@Override
	public Teacher update(@Valid Teacher teacher) {
		int ret =this.teacherMapper.updateById(teacher);
		return this.teacherMapper.selectById(teacher.getId());
	}

	@Override
	public boolean checkTeacherPhoneExist4Add(Teacher teacher) {
		QueryWrapper<Teacher> queryWrapper = new QueryWrapper<Teacher>();
		queryWrapper.lambda().eq(Teacher::getTeacherPhone, teacher.getTeacherPhone());
		int count = this.teacherMapper.selectCount(queryWrapper);
		return count == 0 ? false : true;
	}

	@Override
	public boolean checkTeacherPhoneExist4Edit(Teacher teacher) {
		QueryWrapper<Teacher> queryWrapper = new QueryWrapper<Teacher>();
		queryWrapper.lambda().eq(Teacher::getTeacherPhone, teacher.getTeacherPhone());
		queryWrapper.lambda().ne(Teacher::getId, teacher.getId());
		int count = this.teacherMapper.selectCount(queryWrapper);
		return count == 0 ? false : true;
	}
	
	@Override
	public Teacher login(Teacher teacher) {
		return this.teacherMapper.login(teacher);
	}

	@Override
	public Teacher selectById(Long id) {
		return this.teacherMapper.selectById(id);
	}

	@Override
	public int editInfo(Object name, Object value,Long id) {
		return this.teacherMapper.editInfo(name,value,id);
	}
	
	
}
